package com.blue.http.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.List;

/**
 * WebsocketConfig解析
 *
 * @author zhengjin
 * @since 1.0 2018年10月26日
 */
public class WebsocketConfigParser extends AbstractSimpleBeanDefinitionParser
{
	public WebsocketConfigParser()
	{
	}

	@Override
	protected Class<?> getBeanClass(Element element)
	{
		return WebsocketConfig.class;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
	{
		try
		{
			NodeList pkgList = element.getElementsByTagNameNS("http://www.blue.com/schema/http", "scan-packages");
			if (pkgList.getLength() > 0)
			{
				Element node = (Element) pkgList.item(0).getChildNodes().item(1);
				List<Object> list = parserContext.getDelegate().parseListElement(node, builder.getRawBeanDefinition());
				builder.addPropertyValue("scanPackages", list);
			}

			builder.addPropertyValue("root", element.getAttribute("root"));
		}
		catch (Exception e)
		{
			parserContext.getReaderContext().error(WebsocketConfig.class.getName() + " 无法创建", element, null, e);
		}
	}
}
